package com.antdesignbackend.backend.service.impl;

import com.antdesignbackend.backend.common.http.HttpClientResult;
import com.antdesignbackend.backend.common.http.HttpClientUtils;
import com.antdesignbackend.backend.config.WebConfig;
import com.antdesignbackend.backend.entity.entityVo.MiddlecommandVo;
import com.antdesignbackend.backend.service.ActionService;
import com.antdesignbackend.backend.service.MiddlecommandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;

/**
 * Created by Administrator on 2020/7/7.
 */
@Service
public class ActionServiceImpl implements ActionService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    MiddlecommandService middlecommandService;

    @Override
    public String SendAction(String topic, String sendCommand, Date actionTime, int status, String description, Integer appointId, Integer meterId, Integer commandNo, Integer roomId, Integer shopId) {
        String actionUrl = WebConfig.ActionUrl;
        String reulst = "Error";

        long ts = System.currentTimeMillis();
        String key = ts+"";
        MiddlecommandVo model = new MiddlecommandVo();
        try
        {
            model.setTopic(topic);
            model.setSendCommand(sendCommand);
            model.setCreateTime(new Date());
            model.setActionTime(actionTime);
            model.setStatus(status);
            model.setDescription(description);
            model.setRoomId(roomId);
            model.setShopId(shopId);
            model.setAppointId(appointId);
            model.setMeterId(meterId);
            model.setCommandNo(commandNo);
            model.setUid(key);
            boolean b=middlecommandService.save(model);
            if (status == 1 && actionTime.before(new Date()))
            {
               HttpClientResult httpclient= HttpClientUtils.doGet(String.format("%s?topic=%s&command=%s&uid=%s",actionUrl,model.getTopic(),model.getSendCommand(),key));
            }
            reulst = "OK";
        }
        catch(Exception ex)
        {
            logger.error(ex.getMessage());
        }

        return reulst;

    }
    @Override
    public String SendActiondm(String topic, String sendCommand, Date actionTime, int status, String description, Integer appointId, Integer meterId, Integer commandNo, Integer roomId, Integer shopId,String channellist) {
        String actionUrl = "http://120.77.229.46:9999/sendcmd";
        String reulst = "Error";
        long ts = System.currentTimeMillis();
        String key = ts+"";
        MiddlecommandVo model = new MiddlecommandVo();
        String command="";
        if(commandNo.equals(999)){
            command="http://120.77.229.46:9999/sendcmd?devcode="+topic+"&keyid="+sendCommand+"&ordercode="+key+"&channellist="+channellist;
        }else {
            command="http://120.77.229.46:9999/sendcmd?devcode="+topic+"&keyid="+sendCommand+"&ordercode="+key+"&channel="+commandNo+"&channeltype=1";
        }
        try
        {
            model.setTopic(topic);
            model.setSendCommand(command);
            model.setCreateTime(new Date());
            model.setActionTime(actionTime);
            model.setStatus(status);
            model.setDescription(description);
            model.setRoomId(roomId);
            model.setShopId(shopId);
            model.setAppointId(appointId);
            model.setMeterId(meterId);
            model.setCommandNo(commandNo);
            model.setUid(key);
            boolean b=middlecommandService.save(model);
            if (status == 1 && actionTime.before(new Date())) {
                if(commandNo==999){
                    HttpClientResult httpclient= HttpClientUtils.doPost(String.format("%s?devcode=%s&keyid=%s&ordercode=%s&channellist=%s",actionUrl,model.getTopic(),sendCommand,key,channellist));
                    System.out.println(httpclient);
                }else {
                    HttpClientResult httpclient= HttpClientUtils.doPost(String.format("%s?devcode=%s&keyid=%s&ordercode=%s&channel=%s&channeltype=1",actionUrl,model.getTopic(),sendCommand,key,commandNo));
                    System.out.println(httpclient);
                }
            }
            reulst = "OK";
        }
        catch(Exception ex)
        {
            logger.error(ex.getMessage());
        }
        return reulst;

    }


}
